package com.ewhale.feitengguest.ui.user.task.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ewhale.feitengguest.R;
import com.simga.library.utils.glide.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * PackageName : cn.ewhale.xinnongtong.ui.mine.adapter
 * Author : SimGa Liu
 * Date : 2017/09/23
 * Time : 10:46
 */
public class PublicPicAdapter extends RecyclerView.Adapter<PublicPicAdapter.ViewHolder> {
    private onItemClickListener onItemClickListener;
    private int picNum;
    private List<String> mData;

    public PublicPicAdapter(List<String> mData, int picNum) {
        this.mData = mData;
        this.picNum = picNum;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_select, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == mData.size()) {
            holder.delete.setVisibility(View.GONE);
            holder.image.setImageResource(R.mipmap.b1_btn_camera);
        } else {
            holder.delete.setVisibility(View.VISIBLE);
            GlideUtil.loadPicture(mData.get(position), holder.image);
        }
    }

    @Override
    public int getItemCount() {
        if (mData.size() == picNum) {
            return mData.size();
        }
        return mData.size() + 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.delete)
        ImageView delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onDelete(getLayoutPosition());
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    public void setOnItemClickListener(PublicPicAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface onItemClickListener {
        void onDelete(int position);
        void onItemClick(int position);
    }
}
